/**  定义Exercises相关的接口 */
const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
// 引入mysql连接池
const pool = require("../utils/db.js");

// 新增已激活题目
router.post("/exer-done/add", (req, resp) => {
  let obj = req.body
  let sql = "insert into exer_done set ?"
  pool.query(sql, [obj], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 更新激活题状态
router.post("/exer-done/update", (req, resp) => {
  let obj = req.body
  let sql = "update exer_done set ? where done_id=?";
  pool.query(sql, [obj, obj.done_id], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户已激活题目 (按激活时间排序)
router.get("/exer-done/0", (req, resp) => {
  let {userId} = req.query
  let sql = 'select * from exer_done where userId=?'
  pool.query(sql, [userId], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户已激活号
router.get("/exer-done/3", (req, resp) => {
  let obj = req.query
  let sql = 'select done_id from exer_done where userId=? and exerId=?'
  pool.query(sql, [obj.userId,obj.exerId], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户已激活科目题目
router.get("/exer-done/1", (req, resp) => {
  let obj = req.query
  let sql = 'select * from exer_done where userId=? and done_subj=?'
  pool.query(sql, [obj.userId,obj.done_subj], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户已激活题目 (按题库序号排序)
router.get("/exer-done/2", (req, resp) => {
  let obj = req.query
  let sql = 'select * from exer_done order by exerId asc where userId=?'
  pool.query(sql, [obj.userId], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户做错的题目 (科目)
router.get("/exer-done/wrong", (req, resp) => {
  let obj = req.query
  let sql = 'select * from exer_done where userId=? and done_state=? and done_subj=?'
  pool.query(sql, [obj.userId,obj.done_state,obj.done_subj], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

// 查询用户收藏的题目
router.get("/exer-done/collect", (req, resp) => {
  let obj = req.query
  let sql = 'select * from exer_done where userId=? and done_collect=1'
  pool.query(sql, [obj.userId], (error, result) => {
    if (error) {
      resp.send(Response.error(500, error));
      throw error;
    }
    resp.send(Response.ok(result));
  })
});

module.exports = router;